package cn.iocoder.yudao.module.system.controller.admin.equivalentscorerank;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.dal.dataobject.equivalentscorerank.EquivalentScoreRankDO;
import cn.iocoder.yudao.module.system.dal.dataobject.equivalentscorerank.EquivalentScoreDetailDO;
import cn.iocoder.yudao.module.system.service.equivalentscorerank.EquivalentScoreRankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 等效位次和等效分 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 等效位次和等效分")
@RestController
@RequestMapping("/system/equivalent-score-rank")
@Validated
@Slf4j
public class EquivalentScoreRankController {

    @Resource
    private EquivalentScoreRankService equivalentScoreRankService;

    @PostMapping("/calculate")
    @Operation(summary = "计算学生的等效位次和等效分")
    public CommonResult<EquivalentScoreRankDO> calculateEquivalentScoreRank(
            @Parameter(description = "考试ID", required = true) @RequestParam Long examId,
            @Parameter(description = "学生ID", required = true) @RequestParam Long studentId,
            @Parameter(description = "特控线ID", required = true) @RequestParam Long controlLineId) {
        
        EquivalentScoreRankDO result = equivalentScoreRankService.calculateEquivalentScoreRank(examId, studentId, controlLineId);
        return success(result);
    }

    @PostMapping("/batch-calculate")
    @Operation(summary = "批量计算班级学生的等效位次和等效分")
    public CommonResult<Integer> batchCalculateEquivalentScoreRank(
            @Parameter(description = "考试ID", required = true) @RequestParam Long examId,
            @Parameter(description = "特控线ID", required = true) @RequestParam Long controlLineId) {
        
        int successCount = equivalentScoreRankService.batchCalculateEquivalentScoreRank(examId, controlLineId);
        return success(successCount);
    }

    @GetMapping("/get")
    @Operation(summary = "获取学生的等效位次和等效分记录")
    @Parameter(name = "examId", description = "考试ID", required = true)
    @Parameter(name = "studentId", description = "学生ID", required = true)
    @Parameter(name = "controlLineId", description = "特控线ID")
    public CommonResult<EquivalentScoreRankDO> getEquivalentScoreRank(
            @RequestParam Long examId,
            @RequestParam Long studentId,
            @RequestParam(required = false) Long controlLineId) {
        
        EquivalentScoreRankDO result;
        if (controlLineId != null) {
            result = equivalentScoreRankService.getEquivalentScoreRank(examId, studentId, controlLineId);
        } else {
            result = equivalentScoreRankService.getEquivalentScoreRank(examId, studentId);
        }
        return success(result);
    }

    @GetMapping("/list-by-student")
    @Operation(summary = "获取学生的所有等效位次和等效分记录")
    @Parameter(name = "studentId", description = "学生ID", required = true)
    public CommonResult<List<EquivalentScoreRankDO>> getEquivalentScoreRanksByStudentId(
            @RequestParam Long studentId) {
        
        List<EquivalentScoreRankDO> result = equivalentScoreRankService.getEquivalentScoreRanksByStudentId(studentId);
        return success(result);
    }

    @GetMapping("/list-by-exam")
    @Operation(summary = "获取考试的所有等效位次和等效分记录")
    @Parameter(name = "examId", description = "考试ID", required = true)
    public CommonResult<List<EquivalentScoreRankDO>> getEquivalentScoreRanksByExamId(
            @RequestParam Long examId) {
        
        List<EquivalentScoreRankDO> result = equivalentScoreRankService.getEquivalentScoreRanksByExamId(examId);
        return success(result);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除等效位次和等效分记录")
    @Parameter(name = "id", description = "记录ID", required = true)
    public CommonResult<Boolean> deleteEquivalentScoreRank(@RequestParam Long id) {
        equivalentScoreRankService.deleteEquivalentScoreRank(id);
        return success(true);
    }

    @DeleteMapping("/delete-by-exam")
    @Operation(summary = "删除考试的所有等效位次和等效分记录")
    @Parameter(name = "examId", description = "考试ID", required = true)
    public CommonResult<Integer> deleteEquivalentScoreRanksByExamId(@RequestParam Long examId) {
        int deletedCount = equivalentScoreRankService.deleteEquivalentScoreRanksByExamId(examId);
        return success(deletedCount);
    }

    @DeleteMapping("/delete-by-student")
    @Operation(summary = "删除学生的所有等效位次和等效分记录")
    @Parameter(name = "studentId", description = "学生ID", required = true)
    public CommonResult<Integer> deleteEquivalentScoreRanksByStudentId(@RequestParam Long studentId) {
        int deletedCount = equivalentScoreRankService.deleteEquivalentScoreRanksByStudentId(studentId);
        return success(deletedCount);
    }

    @PostMapping("/recalculate")
    @Operation(summary = "重新计算等效位次和等效分")
    @Parameter(name = "province", description = "省份", required = true)
    @Parameter(name = "examType", description = "考试类型", required = true)
    public CommonResult<Integer> recalculateEquivalentScoreRank(
            @RequestParam String province,
            @RequestParam String examType) {
        
        int recalculatedCount = equivalentScoreRankService.recalculateEquivalentScoreRank(province, examType);
        return success(recalculatedCount);
    }

    @GetMapping("/validate")
    @Operation(summary = "验证等效位次和等效分计算的前置条件")
    @Parameter(name = "examId", description = "考试ID", required = true)
    @Parameter(name = "studentId", description = "学生ID", required = true)
    @Parameter(name = "controlLineId", description = "特控线ID", required = true)
    public CommonResult<String> validateCalculationPreconditions(
            @RequestParam Long examId,
            @RequestParam Long studentId,
            @RequestParam Long controlLineId) {
        
        String validationMessage = equivalentScoreRankService.validateCalculationPreconditions(examId, studentId, controlLineId);
        return success(validationMessage);
    }

    @GetMapping("/details")
    @Operation(summary = "获取等效分数详情列表")
    @Parameter(name = "equivalentScoreRankId", description = "等效分数排名记录ID", required = true)
    public CommonResult<List<EquivalentScoreDetailDO>> getEquivalentScoreDetails(
            @RequestParam Long equivalentScoreRankId) {
        List<EquivalentScoreDetailDO> details = equivalentScoreRankService.getEquivalentScoreDetails(equivalentScoreRankId);
        return success(details);
    }

    @GetMapping("/detail")
    @Operation(summary = "获取指定年份的等效分数详情")
    @Parameter(name = "equivalentScoreRankId", description = "等效分数排名记录ID", required = true)
    @Parameter(name = "year", description = "年份", required = true)
    public CommonResult<EquivalentScoreDetailDO> getEquivalentScoreDetail(
            @RequestParam Long equivalentScoreRankId,
            @RequestParam Integer year) {
        EquivalentScoreDetailDO detail = equivalentScoreRankService.getEquivalentScoreDetail(equivalentScoreRankId, year);
        return success(detail);
    }

    @PostMapping("/calculate-by-input")
    @Operation(summary = "根据用户输入参数计算等效位次和等效分")
    @Parameter(name = "province", description = "省份", required = true)
    @Parameter(name = "examType", description = "选科类型（如：物理类、历史类）", required = true)
    @Parameter(name = "examScore", description = "考试分数", required = true)
    @Parameter(name = "controlLineScore", description = "特控线分数", required = true)
    public CommonResult<Map<Integer, Map<String, Object>>> calculateEquivalentScoreRankByInput(
            @RequestParam String province,
            @RequestParam String examType,
            @RequestParam BigDecimal examScore,
            @RequestParam BigDecimal controlLineScore) {
        
        Map<Integer, Map<String, Object>> result = equivalentScoreRankService.calculateEquivalentScoreRankByInput(
                province, examType, examScore, controlLineScore);
        return success(result);
    }

}